import { LocalstorageDao, localstorageDao } from './../../utils/localstorage';
import { Subscription } from 'rxjs';
import { MessageBoxService, GrowlMessageType } from './../../components/messageBox';
import { MessageHandleManager } from './../../utils/messageHandle/messageHandleManager';
import { FNCManager } from './../../utils/fnc/fncManager';
import { StoreSvr } from './../../services/store';
import { AppStore, System } from './../../services/appStore';
import { NavigateService } from './../../services/navigate';
import { Component, Directive, ElementRef, OnInit } from '@angular/core';



@Component({
    styleUrls: ['./initialization.scss'],
    template: `
        <div  class="checkenv-container ">
            <header>
                <i class="material-icons back divvma" (click)="onBack()">arrow_back</i>
                <h1 class="padding">检测店铺配置环境</h1>
                <div class="nav">
                    <i ></i>
                    <i class="active">2</i>
                    <i ></i>
                    <i ></i>
                    <i ></i>
                </div>
            </header>
            <div class="store margin" >
                <div *ngFor="let store of stores" (click)="selectStore(store)" class="store-info layout-row layout-center">
                    <img src={{store.merchant.logo}} />
                    <div>
                        <p>{{store.branchName}}</p>
                        <p><span>{{store.merchant.name}}</span></p>
                    </div>
                </div>
            </div>
        </div>
    `,
    providers: []
})
//  <div class="failed" *ngSwitchCase='"failed"'>
//<div  *ngSwitchCase='"passed"'>环境检测成功</div>
//<div *ngIf="!store">正在获取店铺信息</div>
export class CheckEnv implements OnInit {
    constructor(
        private appStore: AppStore,
        private navSvr: NavigateService,
        private storeSvr: StoreSvr
    ) {

    }
    private checkStatus: string = "init";
    private system;
    private stores;
    private subscribetion: Subscription;
    private ldcPosTerminalName: string;
    async ngOnInit() {
        const preLDCs = localstorageDao.get("preLDCs");

        const promises = preLDCs.map(async (x) => {
            try {
                return await this.storeSvr.getStore(x.storeId)
            } catch (error) {
                return null;
            }

        });
        this.stores = (await Promise.all(promises)).filter(x => x);

    }
    onBack() {
        this.navSvr.navigate(["initialization/connectLDC"])
    }
    async selectStore(store) {
        const ldcConfig = await this.storeSvr.getLDCConfig(store.id);
        this.storeSvr.CurrentStore = store;
        this.onNextClick();
    }
    async reCheck() {
        //获取店铺的内容
        try {
            this.checkStatus = "checking"
            this.storeSvr.CurrentStore = this.stores;
            this.startCheckEnv()
        } catch (error) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, error, error.message || error);
        }
    }
    startCheckEnv() {
        this.appStore.systemStore.actions.checkFbsOnce()
    }
    private onNextClick() {
        this.navSvr.navigate(["initialization/remoteLogin"])
    }
    private ngOnDestroy() {
        // this.subscribetion.unsubscribe()
    }
}
